package br.com.massai.model.exceptions;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.ejb.EJBException;

public class SystemException extends EJBException {

	private static final long serialVersionUID = -6444280788815697299L;
	
	private GenericCore core;
	private List<String> parameters;
	
	public SystemException(GenericCore core) {
		super(core.getCode());
		this.core = core;
		this.parameters = new ArrayList<String>();
	}
	
	public SystemException(GenericCore core, Exception e) {
		super(core.getCode(), e);
		this.core = core;
		this.parameters = new ArrayList<String>();
	}
	
	public SystemException(GenericCore core, String ... params) {
		super(core.getCode());
		this.core = core;
		this.parameters = Arrays.asList(params);
	}
	
	public SystemException(GenericCore core, Exception e, String ... params) {
		super(core.getCode(), e);
		this.core = core;
		this.parameters = Arrays.asList(params);
	}
	
	public static final String getPilhaError(Throwable t) {
		StringWriter writer = new StringWriter();
		PrintWriter printWriter = new PrintWriter(writer);
		t.printStackTrace(printWriter);
		return writer.toString();
	}
	
	@Override
	public String toString() {
		StringBuilder str = new StringBuilder(SystemException.class.getSimpleName());
		str.append(" [");
		if(core != null) str.append("error=").append(core).append(", ");
		if(parameters != null) str.append("parameters=").append(parameters);
		str.append("]");
		return str.toString();
	}

	public GenericCore getCore() {
		return core;
	}

	public void setCore(GenericCore core) {
		this.core = core;
	}

	public List<String> getParameters() {
		return parameters;
	}

	public void setParameters(List<String> parameters) {
		this.parameters = parameters;
	}
}